package cn.sunjinxin.trade.domain.service;

import cn.sunjinxin.savior.ext.container.ExtExecutor;
import cn.sunjinxin.savior.ext.container.ExtRo;
import cn.sunjinxin.trade.domain.ability.OrderAbility;
import cn.sunjinxin.trade.domain.enums.OrderType;
import cn.sunjinxin.trade.domain.even.TradeEvent;
import cn.sunjinxin.trade.domain.gateway.OrderGateway;
import cn.sunjinxin.trade.domain.model.Order;
import cn.sunjinxin.trade.domain.model.ProductSpu;
import cn.sunjinxin.trade.domain.model.entity.Distributor;
import cn.sunjinxin.trade.domain.model.entity.Traveler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author issavior
 */
@Service
@Slf4j
public class OrderDomainService {

    @Resource
    private OrderGateway orderGateway;

    public Order initB2bOrder(ProductSpu productSpu, Distributor distributor, List<Traveler> travelers) {
        Order b2bOrder = Order.initOrder(OrderType.B2B, productSpu, distributor, travelers);
        List<Order> childOrders = productSpu.getProductSkus().stream()
                .map(sku -> Order.initOrder(OrderType.B2P, productSpu.of(Collections.singletonList(sku)), distributor, travelers))
                .collect(Collectors.toList());
        b2bOrder.setChildOrders(childOrders);

        return b2bOrder;
    }

    public Order batchSaveOrder(ProductSpu productSpu, Distributor distributor, List<Traveler> travelers) {
        Order b2bOrder = Order.initOrder(OrderType.B2B, productSpu, distributor, travelers);
        List<Order> childOrders = productSpu.getProductSkus().stream()
                .map(sku -> Order.initOrder(OrderType.B2P, productSpu.of(Collections.singletonList(sku)), distributor, travelers))
                .collect(Collectors.toList());
        b2bOrder.setChildOrders(childOrders);

        List<Order> tpOrders = new ArrayList<>();
        tpOrders.add(b2bOrder);
        tpOrders.addAll(b2bOrder.getChildOrders());

        orderGateway.batchSave(tpOrders);
        return b2bOrder;
    }

    public Order preOrder(OrderType orderType, ProductSpu productSpu, Distributor distributor, List<Traveler> travelers) {
        Order b2bOrder = Order.initOrder(OrderType.B2B, productSpu, distributor, travelers);
        orderGateway.save(b2bOrder);
        productSpu.getProductSkus().forEach(sku -> {
            Order b2pOrder = Order.initOrder(OrderType.B2P, productSpu.of(Collections.singletonList(sku)), distributor, travelers);
            orderGateway.save(b2pOrder);
        });

        return b2bOrder;
    }

    public Order createOrder(ProductSpu productSpu, Distributor distributor, List<Traveler> travelers) {
        return new ExtExecutor<>(OrderAbility.class).execFirst(ExtRo.builder()
                .bizCode("")
                .scenario("")
                .industry("trade")
                .business("order").build(), e -> e.create(productSpu, distributor, travelers));


    }

    public Order pushOrder(TradeEvent tradeEvent, Order tpOrder) {
        return null;
    }

    public Order getB2bOrder(String distributionId, String outOrderId) {
        return orderGateway.getOrderById(null);
    }

}
